#delimit;

clear;
clear matrix;

set memory 900m;
 
set more off;
cap log close; 

#delimit;

global MY_OUT_PATH  "[path where your master data set is stored]"; 
global MY_TEMP_PATH "[path where your temporary files are stored]";
global MY_FIGURE_PATH "[path where your figures and corresponding data sets are stored]";


#delimit;
use "${MY_OUT_PATH}NEW_CNEF3_LSsample.dta", clear;

#delimit;
*********************************************;
*********************************************;
*********************************************;

* check labor force status if working==0;
* lfs is current, while earnings are previous year - Change lfs to previous year;
#delimit;
preserve;
keep persnr year lfp;
replace year=year+1;
rename lfp lfp_new;
save "${MY_TEMP_PATH}changed_lfp", replace;
restore;

merge 1:1 persnr year using "${MY_TEMP_PATH}changed_lfp";
drop _merge;
drop lfp;

tab lfp_new if working==0;

**********************************************;
* sample selection;
*********************************************;
#delimit;

*keep houseeholds in the sample where at least one member is either working or unemployed (at least one member in labor forece)
#delimit;
egen lf=rowtotal(lfp_new working);
bys yearhhnr: egen lfp_hh=total(lf);
keep if lfp_hh>0 & lfp_hh!=.;

* keep only the observation of head (i.e. 1 observation per household);
#delimit;
keep if head==1;

********************************;
* Abbildung: Perzentil-Quotient ;
********************************;

#delimit;
preserve;
collapse (p10) p10HHpreincOECD=HHpreincOECD (p50) p50HHpreincOECD=HHpreincOECD (p90) p90HHpreincOECD=HHpreincOECD [pweight=w11102], by(year);
gen p9050HHpreincOECD=p90HHpreincOECD/p50HHpreincOECD;
gen p1050HHpreincOECD=p10HHpreincOECD/p50HHpreincOECD;
sort year;
list;
save "${MY_FIGURE_PATH}HHincomepercentileratios.dta", replace;
restore;


preserve;
collapse (p10) p10HHpostincOECD=HHpostincOECD (p50) p50HHpostincOECD=HHpostincOECD (p90) p90HHpostincOECD=HHpostincOECD [pweight=w11102], by(year);
gen p9050HHpostincOECD=p90HHpostincOECD/p50HHpostincOECD;
gen p1050HHpostincOECD=p10HHpostincOECD/p50HHpostincOECD;
sort year;
merge 1:1 year using "${MY_FIGURE_PATH}HHincomepercentileratios.dta";
drop _merge;
save "${MY_FIGURE_PATH}HHincomepercentileratios.dta", replace;
restore;

